﻿<?php
if ($level != 3) {
	echo "Bạn không có quyền vào trang này.";
	exit();
}
if($mode == 'broken') {
	$sql_where = "WHERE m_is_broken = 1";
	$sql_order = "ORDER BY m_id DESC";
}
if($search) {
	$sql_where = "WHERE m_title_ascii LIKE '%".$search."%'";
	$sql_order = "ORDER BY m_id DESC";
}
else {
	$sql_order = "ORDER BY m_id DESC";
}
$num = $mysql->query("SELECT * FROM ".$tb_prefix."data $sql_where");
$total = $mysql->num_rows($num);
if($total) {
	$m_per_page = 30;
	if (!$pg) $pg = 1;
	$sql = "SELECT * FROM ".$tb_prefix."data $sql_where $sql_order LIMIT ".(($pg-1)*$m_per_page).",".$m_per_page;
	$query = $mysql->query($sql);
		while($row = $mysql->fetch_array($query)) {
			$song_ID		= $row['m_id'];
			$song_NAME	 	= $row['m_title'];
			$singer_ID 		= $row['m_singer'];
			$singer_NAME 	= m_get_data('SINGER',$singer_ID);
			$song_BROKEN 	= ($row['m_is_broken'] == 0)?'':'<font color=red>X</font>';
			$list_HTML 	   .= "<tr><td><input class=checkbox type=checkbox id=checkbox onclick=docheckone() name=checkbox[] value=$song_ID></td><td class=fr><a href='?act=song&mode=edit&id=$song_ID'><b>".$song_NAME."</b></a></td><td class=fr_2 align=center><b><a href=?act=singer&mode=edit&id=$singer_ID>$singer_NAME</a></b></td><td align=center>$song_BROKEN</td></tr>";
			$view_pages 	= admin_viewpages($total,$m_per_page,$pg);
		}
}
if(!$total) {
	$list_HTML = "<tr><td></td><td align=center>Chưa có bài hát</td></tr>";
}
elseif ($_POST['do']) {
	$arr = $_POST['checkbox'];
	if (!count($arr)) die('Lỗi');
	if ($_POST['selected_option'] == 'del') {
		$in_sql = implode(',',$arr);
		$mysql->query("DELETE FROM ".$tb_prefix."data WHERE m_id IN (".$in_sql.")");
		echo "<script language='JavaScript'>{ window.parent.location='?act=list_song' }</script>";
	}		
	if ($_POST['selected_option'] == 'multi_edit') {
		$arr = implode(',',$arr);
		header("Location: ./?act=song_multi_edit&id=".$arr);
	}
	elseif ($_POST['selected_option'] == 'normal') {
		$in_sql = implode(',',$arr);
		$mysql->query("UPDATE ".$tb_prefix."data SET m_is_broken = 0 WHERE m_id IN (".$in_sql.")");
		echo "<script language='JavaScript'>{ window.parent.location='?act=list_song' }</script>";
	}
	exit();
}
include("templates/list_song.html");
?>